<?php


namespace App\Repositories;


use App\Models\Product as Model;
use App\Traits\Logs;
use LTools\Repositories\RepositoryAbstract;
use LTools\Traits\CodeTrait;

class Product extends RepositoryAbstract
{
    use CodeTrait, Logs;

    /**
     * Product constructor.
     * @param Model $model
     * @author luffyzhao@vip.126.com
     */
    public function __construct(Model $model)
    {
        $this->model = $model;
    }

    /**
     * @param array $attributes
     * @return bool|\Illuminate\Database\Eloquent\Model|void
     * @author luffyzhao@vip.126.com
     */
    public function create(array $attributes = [])
    {
        /** @var \App\Models\Customer $customer */
        $customer = app('customer');
        $attributes['unique'] = $this->unique(6, 'P' . $customer->getAttribute('code'));
        /** @var Model $model */
        $model = parent::create($attributes);
        if (array_key_exists('images', $attributes) && !empty($attributes['images'])) {
            $model->images()->createMany(
                array_map(function ($item) {
                    return ['url' => $item];
                }, $attributes['images'])
            );
        }
        /** 记录日志 */
        $this->recordLogs($model, '创建产品成功');

        return $model;
    }


    /**
     * @param $id
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|null
     */
    public function edit($id)
    {
        return $this->model->with(['images', 'hsCodeModel'])->find($id);
    }


    /**
     * @param $id
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|null
     */
    public function show($id)
    {
        return $this->model->with(['images', 'hsCodeModel', 'logs.user'])->find($id);
    }

    /**
     * @param $id
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|null
     */
    public function record($id)
    {
        return $this->model->with(['hsCodeModel'])->find($id,
            ['id', 'element', 'name', 'hs_code', 'qy1', 'qy2', 'unit', 'country', 'unique', 'net_weight']);
    }

    /**
     * @param $id
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|null
     * @throws \Throwable
     */
    public function upLock($id)
    {
        /** @var Model $model */
        $model = $this->lock($id);

        if ($model->isNormal()) {
            $model->setAttribute('status', 4);
            $model->saveOrFail();

            $this->recordLogs($model, '锁定产品成功');
            return $model;
        }
        throw new \Exception("正常的产品才可以锁定 ！");
    }


    /**
     * @param $attributes
     * @return mixed
     */
    public function select($attributes)
    {
        return $this->model->where($attributes)->limit(10)->get(['id as code', 'name']);
    }

    /**
     * @param $id
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|null
     * @throws \Throwable
     */
    public function unlock($id)
    {
        /** @var Model $model */
        $model = $this->lock($id);

        if ($model->isLock()) {
            $model->setAttribute('status', 3);
            $model->saveOrFail();

            $this->recordLogs($model, '解锁产品成功');
            return $model;
        }
        throw new \Exception("锁定的产品才可以解锁 ！");
    }


    /**
     * @param $id
     * @param array $values
     * @return Model
     * @throws \Exception
     * @throws \Throwable
     */
    public function updateRecord($id, array $values)
    {
        /** @var Model $model */
        $model = $this->lock($id);
        if ($model->isLock()) {
            $model->update($values);

            $this->recordLogs($model, '修改产品备案成功');
            return $model;
        }
        throw new \Exception("只有锁定的产品才可以修改备案信息 ！");
    }

    /**
     * @param $id
     * @return Model
     * @throws \Exception
     * @throws \Throwable
     */
    public function examine($id)
    {
        /** @var Model $model */
        $model = $this->lock($id);
        if ($model->canExamine()) {
            $model->setAttribute('status', 3);
            $model->saveOrFail();

            $this->recordLogs($model, '审核产品成功');
            return $model;
        }
        throw new \Exception("不是审核状态不能审核 ！");
    }

    /**
     * @param $id
     * @param $reject
     * @return Model
     * @throws \Throwable
     */
    public function reject($id, $reject)
    {
        /** @var Model $model */
        $model = $this->lock($id);
        if ($model->canExamine()) {
            $model->setAttribute('status', 2);
            $model->saveOrFail();

            $this->recordLogs($model, '驳回产品:' . $reject);
            return $model;
        }
        throw new \Exception("不是审核状态不能驳回 ！");
    }

    /**
     * @param $id
     * @param array $values
     * @return bool|\Illuminate\Database\Eloquent\Model|void
     * @throws \Throwable
     */
    public function update($id, $values)
    {
        /** @var Model $model */
        $model = $this->lock($id);
        if ($model->canUpdate()) {
            $model->fill($values)->saveOrFail();
            if (array_key_exists('images', $values) && !empty($values['images'])) {
                $model->images()->delete();
                $model->images()->createMany(
                    array_map(function ($item) {
                        return ['url' => $item];
                    }, $values['images'])
                );
            }

            $this->recordLogs($model, '更新产品成功');

            return $model;
        }
        throw new \Exception("只有异常状态和草稿状态的产品可以修改！");
    }
}
